<template>
  <Dialog
    :title="dialogType == 'add' ? '新增' : '修改'"
    :visible="visible"
    @cancel="handleCancel"
    @success="handleSuccess"
  >
    <el-form
      slot="content"
      ref="roleForm"
      :model="roleForm"
      :rules="rules"
      :status-icon="true"
      label-width="100px"
      label-position="right"
    >
      <el-form-item label="角色名称：" prop="name">
        <el-input v-model="roleForm.name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="默认：">
        <el-switch
          v-model="roleForm.isDefault"
          active-text="是"
          inactive-text="否"
        />
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script>
import Dialog from '@/components/Dialog'
export default {
  components: { Dialog },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        isPublic: false,
        isDefault: false
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      },
      editId: ''
    }
  },
  mounted() {},
  methods: {
    handleCancel() {
      const vm = this
      vm.$emit('cancel')
    },
    handleSuccess() {
      const vm = this
      vm.validateForm()
        .then(() => {
          vm.$emit('success', vm.roleForm)
        })
        .catch(err => {
          console.log(err)
        })
    },
    validateForm() {
      const vm = this
      return new Promise((resolve, reject) => {
        vm.$refs.roleForm.validate(valid => {
          if (valid) {
            resolve()
          } else {
            reject()
            return false
          }
        })
      })
    },
    async setData(row) {
      const vm = this
      vm.editId = row.id
      vm.roleForm = {
        name: row.name,
        isPublic: row.isPublic,
        isDefault: row.isDefault,
        concurrencyStamp: row.concurrencyStamp
      }
    },
    resetForm() {
      const vm = this
      vm.$refs.roleForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped></style>
